package com.yhsimon.factory.simplefactory.pizzastore.order;

import com.yhsimon.factory.simplefactory.pizzastore.pizza.CheesePizza;
import com.yhsimon.factory.simplefactory.pizzastore.pizza.GreekPizza;
import com.yhsimon.factory.simplefactory.pizzastore.pizza.Pizza;

import javax.imageio.IIOException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderPizza {

    //构造器
//    public OrderPizza(){
////        Pizza pizza;
////        String orderType;
////        do{
////            orderType=getType();
////            if(orderType.equals("greek")){
////                pizza=new GreekPizza();
////                pizza.setName("GreekPizza");
////            }else if(orderType.equals("cheese")){
////                pizza=new CheesePizza();
////                pizza.setName("CheesePizza");
////            }else{
////                break;
////            }
////            //打印披萨制作过程
////            pizza.process();
////        }while (true);
////    }

    //定义一个简单工厂对象
    SimpleFactory simpleFactory;
    Pizza pizza=null;

    //构造器
    public OrderPizza(SimpleFactory simpleFactory){
        setSimpleFactory(simpleFactory);
    }

    public void setSimpleFactory(SimpleFactory simpleFactory) {
        //获取用户输入披萨类型
        String orderType;

        this.simpleFactory=simpleFactory;

        do {
            orderType=getType();
            pizza=this.simpleFactory.createPizza(orderType);
            if (pizza!=null){
                pizza.process();
            }else{
                System.out.println("披萨订购失败");
                break;
            }
        }while (true);

    }

    //获取客户希望订购的披萨种类
    private String getType(){
        try{
            BufferedReader strIn = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza type：");
            return strIn.readLine();
        }catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
